package com.bsoft.exchange.service;

import com.bsoft.exchange.dao.his.NetPrescriptionMapper;
import com.bsoft.exchange.dao.portal.HospitalInformationMapper;
import com.bsoft.exchange.domain.*;
import com.bsoft.exchange.pojo.AuditNetPrescriptionPojo;
import com.bsoft.exchange.pojo.NetPrescriptionDetailsPojo;
import com.bsoft.exchange.pojo.NetPrescriptionListPojo;
import com.bsoft.exchange.util.*;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 创建日期 : 2019/10/23
 * 创建人 : 赵太行
 */
@Service
public class NetPrescriptionService {
    @Autowired
    NetPrescriptionMapper netPrescriptionMapper;
    @Autowired
    PublicService publicService;
    @Autowired
    IdentityManager identityManager;
    @Autowired
    HospitalInformationMapper hospitalInformationMapper;
    int pageNo;
    int pageSize;
    private List<PharmacyInfoDomain> data3List;

    private List<Integer> xhList;

    /**
     * 获取审方列表
     */
    public BaseResponse getNetPrescriptionListService(NetPrescriptionListPojo pojo) {
        String cfhm = null;
        BaseResponse baseResponse = new BaseResponse();
        if (pojo.getPageNo()==null) {
            pageNo = 1;
        }else{
            pageNo=pojo.getPageNo();
        }
        if (pojo.getPageSize()==null) {
            pageSize = 10;
        }else{
            pageSize=pojo.getPageSize();
        }

        if (!TextUtils.isEmpty(pojo.getKeyWord())) {
            cfhm = "%" + pojo.getKeyWord() + "%";
        }

        xhList = new ArrayList<>();
        try {
//            if (pojo.getPresType() == 1) {
//                //我的审批
//                data2List = netPrescriptionMapper.getCf02List(pojo, null);
//                for (PrescriptionDrugDomain data2 : data2List) {
//                    if (!xhList.contains(data2.getCFSB())) {
//                        xhList.add(data2.getCFSB());
//                    }
//                }
//                PageHelper.startPage(pageNo, pageSize);
//                data1List = netPrescriptionMapper.getCf01List(pojo, cfhm, xhList);
//
//            } else {
//                //我的申请
//                PageHelper.startPage(pageNo, pageSize);
//                data1List = netPrescriptionMapper.getCf01List(pojo, cfhm, null);
//
//                for (NetPrescriptionDomain data1 : data1List) {
//                    if (!xhList.contains(data1.getPrescriptionId())) {
//                        xhList.add(data1.getPrescriptionId());
//                    }
//                }
//                data2List = netPrescriptionMapper.getCf02List(pojo, xhList);
//            }
            List<Integer> departmentList=List2String.stringToList(pojo.getDepartmentCode());
            PageHelper.startPage(pageNo, pageSize);
            List<NetPrescriptionDomain> data1List = netPrescriptionMapper.getCf01List(pojo, cfhm,departmentList);
            if (data1List.size()==0){
                baseResponse.setCode(Constant.successResponse);
                baseResponse.setMessage("未查询到数据");
                return baseResponse;
            }
            for (NetPrescriptionDomain data1 : data1List) {
                if (!xhList.contains(data1.getPrescriptionId())) {
                    xhList.add(data1.getPrescriptionId());
                }
            }
            List<PrescriptionDrugDomain> data2List = netPrescriptionMapper.getCf02List(pojo, xhList);
            for (NetPrescriptionDomain data1 : data1List) {
                List<PrescriptionDrugDomain> myList = new ArrayList<>();
                for (PrescriptionDrugDomain data2 : data2List) {
                    if (data2.getCFSB().equals(data1.getPrescriptionId())) {
                        myList.add(data2);
                    }
                }
                data1.setPrescriptionDrugList(myList);
                if (data1.getBRXB() == null) {
                    data1.setPatientSex("未知");
                } else {
                    if (data1.getBRXB() == 1) {
                        data1.setPatientSex("男");
                    } else {
                        data1.setPatientSex("女");
                    }
                }
                if (myList.size() > 0) {
                    data1.setAuditStatus(myList.get(0).getSFJG());
                }
            }
            baseResponse.setCode(Constant.successResponse);
            baseResponse.setMessage("查询成功");
            baseResponse.setData(data1List);
            xhList = null;
        } catch (Exception e) {
            e.printStackTrace();
            baseResponse.setCode(Constant.errorTag);
            baseResponse.setMessage("数据库错误");
        }

        return baseResponse;
    }

    /**
     * 获取审方详情
     */

    public BaseResponse getNetPrescriptionDetailsService(NetPrescriptionDetailsPojo pojo) {
        BaseResponse baseResponse = new BaseResponse();
        PrescriptionDetailsDomain domain = netPrescriptionMapper.getDetails(pojo.getOrganizationId(), pojo.getDoctorId(),
                pojo.getPrescriptionId());

        if (domain == null) {
            baseResponse.setCode(Constant.successResponse);
            baseResponse.setMessage("暂无数据");
            return baseResponse;
        }
        PatientInfoDomain patientInfoDomain = netPrescriptionMapper.getPatient(domain.getPatientId(), domain.getDoctorId(), domain.getKSDM(), pojo.getOrganizationId());
        if (patientInfoDomain == null) {
            baseResponse.setCode(Constant.successResponse);
            baseResponse.setMessage("暂无数据");
            return baseResponse;
        }
        List<String> gmlb = netPrescriptionMapper.getGmypList(pojo.getOrganizationId(), domain.getPatientId());
        String gmyp = List2String.listToString(gmlb);
        if (patientInfoDomain.getBRXB() == null) {
            domain.setPatientSex("未知");
        } else {
            if (patientInfoDomain.getBRXB() == 1) {
                domain.setPatientSex("男");
            } else {
                domain.setPatientSex("女");
            }
        }
        domain.setPatientAge(patientInfoDomain.getBRNL());
        domain.setPrescriptionDoctorName(patientInfoDomain.getYGXM());
        domain.setOutpatientNumber(patientInfoDomain.getMZHM());
        domain.setDepartmentName(patientInfoDomain.getKSMC());
        domain.setIrritabilityDrug(gmyp);
//        domain.setAuditStatus(domain.getPrescriptionDrugList().get(0).getSFJG());
//        domain.setAuditMind(domain.getPrescriptionDrugList().get(0).getSFYJ());
        List<HospitalInformation> hospitalInformationList=hospitalInformationMapper.selectList(pojo.getOrganizationId());
        for (HospitalInformation yymc:hospitalInformationList){
            if (yymc.getOrganizationId().equals(domain.getOrganizationId())){
                domain.setOrganizationName(yymc.getOrganizationName());
            }
        }
        List<DoctorInfo> sfyg = publicService.getDoctorInfo(pojo.getOrganizationId());//获取员工信息
        List<DrugInfo> drugInfoList=publicService.getDrugInfo();
        List<BrxzInfo> brxzList=publicService.getBrxzList();
        for (PrescriptionDrugDomain prescriptionDrugDomain : domain.getPrescriptionDrugList()) {

            for (DrugInfo drugInfo:drugInfoList){
                if (drugInfo.getYPXH().equals(prescriptionDrugDomain.getDrugCode())){
                    prescriptionDrugDomain.setDrugName(drugInfo.getYPMC());
                    break;
                }
            }
        }
        for (BrxzInfo brxzInfo:brxzList){
            if (brxzInfo.getBRXZ().equals(domain.getBRXZ())){
                domain.setFeeNature(brxzInfo.getXZMC());
            }
        }
        List<SfjgDomain> sfjgDomain=netPrescriptionMapper.getNetPresctiptiInfo(pojo.getOrganizationId(),domain.getPrescriptionId());
        if (sfjgDomain.size()>0) {
            if (!TextUtils.isEmpty(sfjgDomain.get(0).getSFGH())) {
                for (DoctorInfo doctorInfo : sfyg) {
                    if (doctorInfo.getYGDM().equals(sfjgDomain.get(0).getSFGH())) {
                        domain.setAuditor(doctorInfo.getYGXM());
                        break;
                    }
                }
            }
            domain.setAuditStatus(sfjgDomain.get(0).getSFJG());
            domain.setAuditMind(sfjgDomain.get(0).getSFYJ());
            domain.setAuditDate(sfjgDomain.get(0).getSFSJ());
        }else{
            domain.setAuditStatus(0);
        }
        baseResponse.setCode(Constant.successResponse);
        baseResponse.setMessage("查询成功");
        baseResponse.setData(domain);
        return baseResponse;
    }

    /**
     * 提交审方结果
     */
    @Transactional(value="hisTransactionManager",rollbackFor = Exception.class)
    public BaseResponse auditPrescriptionData(AuditNetPrescriptionPojo pojo) throws Exception {
        BaseResponse baseResponse = new BaseResponse();
        if (pojo.getAuditStatus().equals(2)) {
            if (TextUtils.isEmpty(pojo.getAuditMind())) {
                baseResponse.setCode(Constant.errorTag);
                baseResponse.setMessage("审核不通过时审核意见不能为空");
                return baseResponse;
            }
        }
        if (pojo.getAuditStatus().equals(0)) {
            baseResponse.setCode(Constant.errorTag);
            baseResponse.setMessage("提交的审核状态不能是待审核");
            return baseResponse;
        }
        Integer number=netPrescriptionMapper.getCf02Number(pojo.getOrganizationId(),pojo.getPrescriptionId());
        if (number>0){
//            long jlxh=identityManager.getMax("YF_MZ_SFJL",1);
            netPrescriptionMapper.auditPrescription(pojo);
                //获取cf02记录
            String time = TimeUtil.getNowDate();

                List<SfjgDomain> netPresctiptiInfo=netPrescriptionMapper.getNetPresctiptiInfo(pojo.getOrganizationId(), pojo.getPrescriptionId());
                if (netPresctiptiInfo.size()>0){
                    //更新审方记录表
                    netPrescriptionMapper.updateSfjlData(netPresctiptiInfo.get(0).getSFCS()+1,pojo.getAuditStatus(),
                            pojo.getAuditMind(),time,pojo.getOrganizationId(), pojo.getPrescriptionId());
                }else {
                    //插入审方记录表
                    List<Cf02Info> cf02InfoList = netPrescriptionMapper.getCf02Info(pojo.getOrganizationId(), pojo.getPrescriptionId());
                    long jlxh=identityManager.getMax("YF_MZ_SFJL",cf02InfoList.size());
                    for (Cf02Info cf02Info : cf02InfoList) {
                        cf02Info.setJGID(pojo.getOrganizationId());
                        cf02Info.setSFBZ(pojo.getAuditStatus());
                        cf02Info.setSFYJ(pojo.getAuditMind());
                        cf02Info.setSFGH(pojo.getAuditDoctorCode());
                        cf02Info.setSFSJ(time);
                        cf02Info.setJLXH(jlxh);
                        jlxh++;
                    }
                    netPrescriptionMapper.insertSfjl(cf02InfoList);
                }
            baseResponse.setCode(Constant.successResponse);
            baseResponse.setMessage("提交成功");
            }else{
                baseResponse.setCode(Constant.successResponse);
                baseResponse.setMessage("未查询到该处方信息");
            }
        return baseResponse;
    }

    /**
     * 重新提交审方结果
     */
    @Transactional(value="hisTransactionManager",rollbackFor = Exception.class)
    public BaseResponse auditRetryPrescriptionData(String organizationId, String prescriptionId) {
        BaseResponse baseResponse = new BaseResponse();
        try {
            Integer number=netPrescriptionMapper.getCf02Number(organizationId,prescriptionId);
            if (number>0){
                netPrescriptionMapper.auditRetryPrescription(organizationId, prescriptionId);
                netPrescriptionMapper.updateSfjl(organizationId, prescriptionId);
            }else{
                baseResponse.setCode(Constant.successResponse);
                baseResponse.setMessage("未查询到该处方信息");
            }

        } catch (SQLException e) {
            e.printStackTrace();
            baseResponse.setCode(Constant.errorTag);
            baseResponse.setMessage("数据库出错");
            return baseResponse;
        }
        baseResponse.setCode(Constant.successResponse);
        baseResponse.setMessage("重新提交成功");
        return baseResponse;
    }

    /**
     * 获取是药房列表
     */
    public BaseResponse getPharmacyListServuce(String organizationId, String doctorId) {
        BaseResponse baseResponse = new BaseResponse();
        try {
            data3List = netPrescriptionMapper.getYFList(organizationId, doctorId);
            if (data3List == null) {
                baseResponse.setCode(Constant.successResponse);
                baseResponse.setMessage("暂无数据");
                return baseResponse;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            baseResponse.setCode(Constant.errorTag);
            baseResponse.setMessage("数据库出错");
            return baseResponse;
        }
        baseResponse.setCode(Constant.successResponse);
        baseResponse.setMessage("查询成功");
        baseResponse.setData(data3List);
        return baseResponse;
    }
}
